package com.liang.controller;

import com.liang.WebSocketChat.WebSocketServer;
import com.liang.common.lang.Result;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @name: WebSocketChatServer
 * @author: 李昂
 * @date: 2020-12-11 21:04
 * @description：@see ServerEndpoint WebSocket服务端 需指定端点的访问路径
 * *            @see Session   WebSocket会话对象 通过它给客户端发送消息
 * @update: 2020-12-11 21:04
 */
@RestController
public class WebSocketChatController {

    //一对一单聊
    @PostMapping("/pushToOne/{toUserId}")
    public Result pushToOne(@RequestParam("message") String message, @PathVariable String toUserId) throws IOException {

        try {
            WebSocketServer.sendInfo(message, toUserId);
            return Result.succ();
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    //群聊消息
    @PostMapping("/pushToAll")
    public Result pushToAll(@RequestParam("message") String message) throws IOException {
        try {
            WebSocketServer.sendMessageToAll(message);
            return Result.succ();
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }

    }


}
